﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.facebook.delegates {
    import flash.events.*;
    import com.facebook.events.*;
    import com.facebook.net.*;
    import flash.net.*;
    import com.facebook.data.*;
    import com.facebook.errors.*;
    import com.facebook.session.*;

    public class WebDelegate extends EventDispatcher implements IFacebookCallDelegate {

        protected var _call:FacebookCall;
        protected var loader:URLLoader;
        protected var _session:WebSession;
        protected var parser:XMLDataParser;
        protected var fileRef:FileReference;

        public function WebDelegate(_arg1:FacebookCall, _arg2:WebSession){
            this.call = _arg1;
            this.session = _arg2;
            this.parser = new XMLDataParser();
            this.execute();
        }
        protected function onDataComplete(_arg1:Event):void{
            this.handleResult((_arg1.target.data as String));
        }
        public function set call(_arg1:FacebookCall):void{
            this._call = _arg1;
        }
        protected function createURLLoader():void{
            this.loader = new URLLoader();
            this.loader.addEventListener(Event.COMPLETE, this.onDataComplete);
            this.loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, this.onHTTPStatus);
            this.loader.addEventListener(IOErrorEvent.IO_ERROR, this.onError);
            this.loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onError);
        }
        protected function clean():void{
            if (this.loader == null){
                return;
            };
            this.loader.removeEventListener(Event.COMPLETE, this.onDataComplete);
            this.loader.removeEventListener(IOErrorEvent.IO_ERROR, this.onError);
            this.loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onError);
        }
        protected function addOptionalArguments():void{
            this.call.setRequestArgument("ss", true);
        }
        public function get session():IFacebookSession{
            return (this._session);
        }
        public function get call():FacebookCall{
            return (this._call);
        }
        protected function post():void{
            this.addOptionalArguments();
            RequestHelper.formatRequest(this.call);
            this.sendRequest();
        }
        protected function sendRequest():void{
            this.createURLLoader();
            var _local1:URLRequest = new URLRequest(this._session.rest_url);
            _local1.contentType = "application/x-www-form-urlencoded";
            _local1.method = URLRequestMethod.POST;
            _local1.data = this.call.args;
            trace(((_local1.url + "?") + unescape(this.call.args.toString())));
            this.loader.dataFormat = URLLoaderDataFormat.TEXT;
            this.loader.load(_local1);
        }
        protected function onError(_arg1:ErrorEvent):void{
            this.clean();
            var _local2:FacebookError = this.parser.createFacebookError(_arg1, this.loader.data);
            this.call.handleError(_local2);
            dispatchEvent(new FacebookEvent(FacebookEvent.COMPLETE, false, false, false, null, _local2));
        }
        public function set session(_arg1:IFacebookSession):void{
            this._session = (_arg1 as WebSession);
        }
        protected function handleResult(_arg1:String):void{
            var _local3:FacebookData;
            this.clean();
            var _local2:FacebookError = this.parser.validateFacebookResponce(_arg1);
            if (_local2 == null){
                _local3 = this.parser.parse(_arg1, this.call.method);
                this.call.handleResult(_local3);
            } else {
                this.call.handleError(_local2);
            };
        }
        protected function execute():void{
            if (this.call == null){
                throw (new Error("No call defined."));
            };
            this.post();
        }
        public function close():void{
            try {
                this.loader.close();
            } catch(e) {
            };
        }
        protected function onHTTPStatus(_arg1:HTTPStatusEvent):void{
        }

    }
}//package com.facebook.delegates 
